package cn.edu.tju.ldj.analytics.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.tju.ldj.analytics.utils.UtilFile;

public class IServiceHistoryDataProcess {
	public static final String[] goodKeySet = {"Date", "Open", "High", "Low", "Close", "Volume"};
	public static final String outPutFilePath = "C:\\Users\\popeliu\\Downloads\\future_history\\";
	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		zhengzhouHistoryData();
//		shanghaiHistoryData();
		dalianHistoryData();
	}
	

	public static void zhengzhouHistoryData(){
		String[] fileNameList = {"C:\\Users\\popeliu\\Downloads\\future_src\\zhengzhou\\datahistory2010.txt",
				"C:\\Users\\popeliu\\Downloads\\future_src\\zhengzhou\\datahistory2011.txt",
				"C:\\Users\\popeliu\\Downloads\\future_src\\zhengzhou\\datahistory2012.txt",
				"C:\\Users\\popeliu\\Downloads\\future_src\\zhengzhou\\datahistory2013.txt",
				"C:\\Users\\popeliu\\Downloads\\future_src\\zhengzhou\\datahistory2014.txt"};
		
		Map<String, List<Map<String, Object>>> goodLst = new HashMap<String, List<Map<String,Object>>>();
		for (int i = 0; i < fileNameList.length; i++) {
			try {
				String encoding = "GBK";
				File file = new File(fileNameList[i]);
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					InputStreamReader read = new InputStreamReader(
							new FileInputStream(file), encoding);// 考虑到编码格式
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					@SuppressWarnings("unused")
					int num =0;
					while ((lineTxt = bufferedReader.readLine()) != null){// && num<30) {
						if(lineTxt.contains("交易日期	") || lineTxt.contains("郑州商品交易所")){
							continue;
						}
						String [] dataStr = lineTxt.split("\\|");
						if(dataStr.length == 15){
							for (int j = 0; j < dataStr.length; j++) {
								dataStr[j] = dataStr[j].replaceAll(",", "").replaceAll("	", "").replaceAll("	", "").replaceAll("	", "");
							}
							String goodName = dataStr[1];
							String goodDate = dataStr[0].replaceAll("-", "");
							String goodOpen = dataStr[3];
							String goodHigh = dataStr[4];
							String goodLow = dataStr[5];
							String goodClose = dataStr[6];
							String goodVolume = dataStr[10];
							
							
							if("0".equals(goodOpen)){
								continue;
							}
							Map<String, Object> newDataItem = new HashMap<String, Object>();
							newDataItem.put(goodKeySet[0], goodDate);
							newDataItem.put(goodKeySet[1], goodOpen);
							newDataItem.put(goodKeySet[2], goodHigh);
							newDataItem.put(goodKeySet[3], goodLow);
							newDataItem.put(goodKeySet[4], goodClose);
							newDataItem.put(goodKeySet[5], goodVolume);
							
							if(goodLst.containsKey(goodName)){
								goodLst.get(goodName).add(newDataItem);
							}else{
								List<Map<String, Object>> newGood = new ArrayList<Map<String,Object>>();
								newGood.add(newDataItem);
								goodLst.put(goodName, newGood);
							}
						}
						else{
							System.out.println(lineTxt);
						}
//						num++;
					}
					read.close();
				} else {
					System.out.println("找不到指定的文件");
				}
			} catch (Exception e) {
				System.out.println("读取文件内容出错");
				e.printStackTrace();
			}
		}
		writeCsv(goodLst, "\\zhengzhou\\");
	}
	
	public static void shanghaiHistoryData(){
		String[] fileNameList = {"C:\\Users\\popeliu\\Downloads\\future_src\\shanghai\\2011price.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\shanghai\\2012price.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\shanghai\\2013price.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\shanghai\\2014price.csv"};
		Map<String, List<Map<String, Object>>> goodLst = new HashMap<String, List<Map<String,Object>>>();
		for (int i = 0; i < fileNameList.length; i++) {
			try {
				String encoding = "GBK";
				File file = new File(fileNameList[i]);
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					InputStreamReader read = new InputStreamReader(
							new FileInputStream(file), encoding);// 考虑到编码格式
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					int num =0;
					String lastGoodName = null;
					while ((lineTxt = bufferedReader.readLine()) != null){// && ) {
						if(num>30){
							break;
						}
						if(lineTxt.contains("合约")){
							continue;
						}
						String [] dataStr = lineTxt.split(",");
						if(dataStr.length == 14){
							for (int j = 0; j < dataStr.length; j++) {
								dataStr[j] = dataStr[j].replaceAll(",", "").replaceAll("	", "").replaceAll("	", "").replaceAll("	", "");
							}
							lastGoodName = dataStr[0].length()>0?dataStr[0]:lastGoodName;
							
							String goodName = lastGoodName;
							String goodDate = dataStr[1].replaceAll("-", "");
							String goodOpen = dataStr[4];
							String goodHigh = dataStr[5];
							String goodLow = dataStr[6];
							String goodClose = dataStr[7];
							String goodVolume = dataStr[11];
							
							
							if("0".equals(goodOpen)|| "".equals(goodOpen)){
								continue;
							}
							Map<String, Object> newDataItem = new HashMap<String, Object>();
							newDataItem.put(goodKeySet[0], goodDate);
							newDataItem.put(goodKeySet[1], goodOpen);
							newDataItem.put(goodKeySet[2], goodHigh);
							newDataItem.put(goodKeySet[3], goodLow);
							newDataItem.put(goodKeySet[4], goodClose);
							newDataItem.put(goodKeySet[5], goodVolume);
							
							if(goodLst.containsKey(goodName)){
								goodLst.get(goodName).add(newDataItem);
							}else{
								List<Map<String, Object>> newGood = new ArrayList<Map<String,Object>>();
								newGood.add(newDataItem);
								goodLst.put(goodName, newGood);
							}
						}
						else{
							System.out.println(lineTxt);
						}
//						num++;
					}
					read.close();
				} else {
					System.out.println("找不到指定的文件");
				}
			} catch (Exception e) {
				System.out.println("读取文件内容出错");
				e.printStackTrace();
			}
		}
		writeCsv(goodLst, "\\shanghai\\");
	}
	
	public static void dalianHistoryData(){
		String[] fileNameList = {
//				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\玉米#2006.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2007.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2008.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2009.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2010.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2011.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2012.csv",
				"C:\\Users\\popeliu\\Downloads\\future_src\\dalian\\棕榈油#2013.csv"};
		String goodFileName = "p";
		Map<String, List<Map<String, Object>>> goodLst = new HashMap<String, List<Map<String,Object>>>();
		for (int i = 0; i < fileNameList.length; i++) {
			try {
				String encoding = "GBK";
				File file = new File(fileNameList[i]);
				if (file.isFile() && file.exists()) { // 判断文件是否存在
					InputStreamReader read = new InputStreamReader(
							new FileInputStream(file), encoding);// 考虑到编码格式
					BufferedReader bufferedReader = new BufferedReader(read);
					String lineTxt = null;
					int num =0;
					String lastGoodName = null;
					while ((lineTxt = bufferedReader.readLine()) != null){// && ) {
						if(num>30){
							break;
						}
						if(lineTxt.contains("合约")){
							continue;
						}
						String [] dataStr = lineTxt.split(",");
						if(dataStr.length == 15){
							for (int j = 0; j < dataStr.length; j++) {
								dataStr[j] = dataStr[j].replaceAll(",", "").replaceAll("\\.00", "").replaceAll("\"", "").replaceAll("	", "");
							}
							lastGoodName = dataStr[1].length()>0?dataStr[1]:lastGoodName;
							
							String goodName = lastGoodName;
							String goodDate = dataStr[2].replaceAll("-", "");
							if(goodDate.length()<8){
								if(Integer.parseInt(goodDate.substring(0,1))>5){
									goodDate = "200" + goodDate;
								}else{
									goodDate = "201" + goodDate;
								}
							}
							
							String goodOpen = dataStr[5];
							String goodHigh = dataStr[6];
							String goodLow = dataStr[7];
							String goodClose = dataStr[8];
							String goodVolume = dataStr[12];
							
							
							if("0".equals(goodOpen)|| "".equals(goodOpen)){
								continue;
							}
							Map<String, Object> newDataItem = new HashMap<String, Object>();
							newDataItem.put(goodKeySet[0], goodDate);
							newDataItem.put(goodKeySet[1], goodOpen);
							newDataItem.put(goodKeySet[2], goodHigh);
							newDataItem.put(goodKeySet[3], goodLow);
							newDataItem.put(goodKeySet[4], goodClose);
							newDataItem.put(goodKeySet[5], goodVolume);
							
							if(goodLst.containsKey(goodName)){
								goodLst.get(goodName).add(newDataItem);
							}else{
								List<Map<String, Object>> newGood = new ArrayList<Map<String,Object>>();
								newGood.add(newDataItem);
								goodLst.put(goodName, newGood);
							}
						}
						else{
							System.out.println(lineTxt);
						}
//						num++;
					}
					read.close();
				} else {
					System.out.println("找不到指定的文件");
				}
			} catch (Exception e) {
				System.out.println("读取文件内容出错");
				e.printStackTrace();
			}
		}
		writeCsv(goodLst, "\\dalian\\", goodFileName);
	}
	
	private static void writeCsv(Map<String, List<Map<String, Object>>> dataLst, String subFile){
		Map<String, String> goodTypeGroup = new HashMap<String, String>();
		for (String key : dataLst.keySet()) {
			String outGoodJsonStr = "\n#" + key + "#";
			for (int i = 0; i < dataLst.get(key).size(); i++) {
				outGoodJsonStr += "\n";
				for (int j = 0; j < goodKeySet.length; j++) {
					outGoodJsonStr += dataLst.get(key).get(i).get(goodKeySet[j]) + ",";
				}
				outGoodJsonStr = outGoodJsonStr.substring(0,outGoodJsonStr.length()-1);
			}
			outGoodJsonStr = outGoodJsonStr.substring(0,outGoodJsonStr.length()-1);
//			System.out.println(key + "--------------\n" + outGoodJsonStr);
			
			if(goodTypeGroup.containsKey(key.substring(0,2))){
				String newStr = (String)goodTypeGroup.get(key.substring(0,2)) + outGoodJsonStr;
				goodTypeGroup.put(key.substring(0,2), newStr);
			}else{
				goodTypeGroup.put(key.substring(0,2), outGoodJsonStr);
			}
		}
		
		for (String key : goodTypeGroup.keySet()) {
			UtilFile.writeFile(outPutFilePath+ subFile +key+".csv", goodTypeGroup.get(key));
		}
	}
	
	private static void writeCsv(Map<String, List<Map<String, Object>>> dataLst, String subFile, String fileName){
		String resultStr = "";
		for (String key : dataLst.keySet()) {
			String outGoodJsonStr = "\n#" + key + "#";
			for (int i = 0; i < dataLst.get(key).size(); i++) {
				outGoodJsonStr += "\n";
				for (int j = 0; j < goodKeySet.length; j++) {
					outGoodJsonStr += dataLst.get(key).get(i).get(goodKeySet[j]) + ",";
				}
				outGoodJsonStr = outGoodJsonStr.substring(0,outGoodJsonStr.length()-1);
			}
			outGoodJsonStr = outGoodJsonStr.substring(0,outGoodJsonStr.length()-1);
//			System.out.println(key + "--------------\n" + outGoodJsonStr);
			
			resultStr += outGoodJsonStr;
		}
		
		UtilFile.writeFile(outPutFilePath+ subFile +fileName+".csv", resultStr);
	}
}
